package com.kakavr.recharge.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Maps;
import com.kakavr.base.controller.BaseController;
import com.kakavr.base.exception.ValidateParamException;
import com.kakavr.recharge.service.IRechargeRuleService;
import com.kakavr.result.MsgResult;
import com.kakavr.result.constant.MsgConstants;
import com.kakavr.util.SessionUtils;
import com.kakavr.util.ValidatorUtils;
/**
 * 平台充值规则管理
 * @author gang.yang
 *
 */
@RestController
@RequestMapping("rechargeRule")
public class RechargeRuleController extends BaseController {
	@Resource
	private IRechargeRuleService rechargeRuleService;

	/**
	 * 添加充值规则
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Map<String, Object> add(HttpServletRequest request) {
		String amount = request.getParameter("amount");
		if (!ValidatorUtils.isPlusMoney(amount)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "充值金额");
		}
		String times = request.getParameter("times");
		if (!ValidatorUtils.isNumber(times)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "次数");
		}
		String regionId = request.getParameter("regionId");
		if (!ValidatorUtils.isNumber(regionId)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "区域");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("amount", amount);
		map.put("times", times);
		map.put("regionId", regionId);
		rechargeRuleService.addRechargeRule(map);
		return MsgResult.okResult();
	}

	/**
	 * 删除充值规则
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	public Map<String, Object> delete(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (!ValidatorUtils.isNumber(id)) {
			throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
					MsgConstants.MSG_CONTENT_1002, "规则");
		}
		Map<String, Object> map = Maps.newHashMap();
		map.put("id", id);
		rechargeRuleService.deleteRechargeRule(map);
		return MsgResult.okResult();
	}

	/**
	 * 充值规则列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "select", method = RequestMethod.GET)
	public Map<String, Object> select(HttpServletRequest request) {
		Map<String, Object> map = Maps.newHashMap();
		Object region = SessionUtils.getCurUser().get("regionId");
		if(null == region){
			String regionId = request.getParameter("regionId");
			if (!ValidatorUtils.isNumber(regionId)) {
				throw new ValidateParamException(MsgConstants.MSG_CODE_1002,
						MsgConstants.MSG_CONTENT_1002, "区域");
			}
			map.put("regionId", regionId);
		}else{
			map.put("regionId", region);
		}
		return MsgResult.okResultData(rechargeRuleService.selectList(map));
	}

}
